package com.usian.controller;

import com.usian.pojo.Emp;
import com.usian.query.EmpDto;
import com.usian.service.EmpService;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/provider")
@RefreshScope //动态从nacos配置中心获取最新值
public class EmpController {

    @Value("${config.info}")
    private String configInfo;


    @Autowired
    private EmpService empService;

    @RequestMapping("findAll")
    public List<Emp> findAll() {
        System.out.println("读取值：" + configInfo);
        return empService.findAll();
    }

    //？get请求
    @GetMapping("findById")
    public Emp findByIds(@RequestParam int id){
        return empService.findById(id);
    }

    @GetMapping("findById/{id}")
    public Emp findById(@PathVariable int id) {
        return empService.findById(id);
    }

    @GetMapping("findByIdAndName/{id}/{name}")
    public Emp findByIdAndName(@PathVariable int id, @PathVariable String name) {
        return empService.findByIdAndName(id, name);
    }

    //@RequestBody:   json串转为java对象
    //@ResponseBody : java对象转为json串    restful风格 传参
    @PostMapping("saveEmp")
    public String saveEmp(@RequestBody  Emp emp){
        empService.saveEmp(emp);
        return "succ";
    }

    @PostMapping("updateEmp")
    public String updateEmp(@RequestBody  Emp emp){
        empService.updateEmp(emp);
        return "succ";
    }
    /**
     * 分页查询列表信息
     *
     * @param empDto
     * @return
     */
    @RequestMapping("findByEmpDto")
    public PageResult<Emp> findByEmpDto(@RequestBody EmpDto empDto){
        return empService.findAll(empDto);
    }


    /**
     * 根据id 物理删除
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public String deleteById(@RequestParam Integer id){
        empService.deleteById(id);
        return "succ";
    }
}
